class Ob {
    constructor(cartId, num) {
        this.cartId = cartId;
        this.num = num;
    }
}

//获取当前登录用户的购物车
let gou = new Vue({
    el: "#cartApp",
    data: {
        parr: null,
        checked_arr: [],
        totalAmount:0,
        selectionList:[{'id':0,'totalPrice':0},{'id':0,'totalPrice':0}]
    },
    //当页面加载完毕时，会自动调用这个方法（vue对象的生命周期方法）*/
    created: function() {
        //发请求到后台获取商品信息
        axios.get("/shoppingsingle/ddCartDetail/list").then(function(response) {
            gou.parr = response.data.data; //把查询到的购物车商品信息给到Vue的变量
        });
    },
    methods: {
        //结算
        jiesuan: function () {
            var arr = new Array();
            for (var i=0;i<this.checked_arr.length;i++){
                //存购物车记录id, 和购买的数量
                arr[i] = new Ob(this.checked_arr[i],document.getElementById("cart_"+this.checked_arr[i]).value);
            }

            axios.post("/shoppingsingle/ddOrderInfo/addOrder",arr).then(function (response) {
                if (response.data.success) {
                    location.href = "/alipay?orderId="+ response.data.data;
                }else{
                    alert("下单失败！");
                }
            });

        },
        amount:function (info) {
            let products={
                'id':info.id,
                'totalPrice':info.discountPrice*info.num
            }
            for (let i=0;i<=gou.selectionList.length;i++){
                let s=gou.selectionList[i].id
                let p=products.id
                if (s==p){
                    gou.selectionList.splice(i-1,1)
                    console.log("删除成功")
                    break
                }else {
                    gou.selectionList.push(products)
                    break
                }
            }

        }
    }

});